package translation;

import java.io.*;

/**
 * Created by xwj on 2017/9/18.
 */
public class Translation {

    public static void translate8(int PART_COUNT,int part,int frameMax,int width,int height,String fileName,String outputFile){
        FileInputStream inputStream=null;
        FileOutputStream outputStream=null;
        byte[] bufY=new byte[width];
        byte[] bufYWrite=new byte[width];
        byte[] bufUV=new byte[width/2];
        byte[] bufUVWrite=new byte[width/2];
        int num=0;
        try{
            inputStream = new FileInputStream(new File(fileName));
            outputStream = new FileOutputStream(new File(outputFile));
            int count=0;
            for(int frame=0;frame<frameMax;frame++) {

                int offset = (int) (width * part / PART_COUNT);
                for (int i = 0; i < height; i++) {
                    if ((num = inputStream.read(bufY)) != -1) {
                        //System.out.println(num);
                        for (int j = 0; j < width; j++)
                            bufYWrite[j] = bufY[(j + offset) % width];
                        outputStream.write(bufYWrite);
                        count+=bufUVWrite.length;
                        outputStream.flush();

                    } else {
                        System.out.println(fileName +"  " +frame+ " Y-" + i + " read fail!!");
                    }
                }

                for (int uv = 0; uv < 2; uv++) {
                    offset = (int) (width / 2 * part / PART_COUNT);
                    for (int i = 0; i < (height / 2); i++) {
                        if ((num = inputStream.read(bufUV)) != -1) {
                            //System.out.println(num);
                            for (int j = 0; j < width / 2; j++)
                                bufUVWrite[j] = bufUV[(j + offset) % (width / 2)];
                            outputStream.write(bufUVWrite);
                            count+=bufUVWrite.length;
                            outputStream.flush();
                        } else {
                            System.out.println(fileName +"  " +frame+ " UV-" + i + " read fail!!");
                        }
                    }
                }
                System.out.println(fileName+"  --> "+outputFile+"     "+frame+"      "+count );

            }

        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try{
                inputStream.close();
                outputStream.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }


    /**
     * @param args
     */
    public static void main(String[] args) {

        for(int part=2;part<8;part++) {
            //int part = 1;
            int partCount = 8;
            int frameMax = 300;
            int width = 3840;
            int height = 1920;
            String fileName = "DrivingInCity_3840x1920_30fps_8bit_420_erp.yuv";
            String outputFile = "DrivingInCity_3840x1920_" + part + "_" + partCount + ".yuv";
            translate8(partCount, part, frameMax, width, height, fileName, outputFile);
        }


    }

}
